﻿using System;
using System.Windows.Input;

namespace MetroWPF.Windows
{
    internal class WindowCommand : ICommand
    {
        /// <summary>
        /// The can execute func
        /// </summary>
        private Func<bool> canExecuteFunc;

        /// <summary>
        /// Initializes a new instance of the <see cref="WindowCommand"/> class.
        /// </summary>
        /// <param name="execute">The execute.</param>
        /// <param name="canExecute">The can execute.</param>
        public WindowCommand(Action execute, Func<bool> canExecute)
        {
            this.ExecuteAction = execute;
            this.CanExecuteFunc = canExecute;
        }

        /// <summary>
        /// Occurs when changes occur that affect whether or not the command should execute.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Gets or sets the can execute func.
        /// </summary>
        /// <value>
        /// The can execute func.
        /// </value>
        public Func<bool> CanExecuteFunc
        {
            get { return canExecuteFunc; }
            set
            {
                canExecuteFunc = value;
                if (this.CanExecuteChanged != null)
                {
                    this.CanExecuteChanged(this, new EventArgs());
                }
            }
        }

        /// <summary>
        /// Gets or sets the execute action.
        /// </summary>
        /// <value>
        /// The execute action.
        /// </value>
        public Action ExecuteAction { get; set; }

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        public bool CanExecute(object parameter)
        {
            return this.CanExecuteFunc();
        }

        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            this.ExecuteAction();
        }
    }
}